<template>
  <transition name="fadeDown">
    <div v-if="showNoticeScroll">
      <van-notice-bar
        :scrollable="false"
        :class="[hasBotGap ? 'cus-bar' : '']"
        background="#ffede4"
        color="#FF793C"
        mode="link"
        left-icon="volume"
        @click="noticeTap"
      >
        <van-swipe
          vertical
          class="notice-swipe"
          :autoplay="3000"
          :show-indicators="false"
          @change="onChange"
        >
          <van-swipe-item v-for="(item, index) in list" :key="index">{{
            item.title
          }}</van-swipe-item>
        </van-swipe>
      </van-notice-bar>

      <van-popup
        class="modal-transparent"
        v-model:show="popShow"
        :close-on-click-overlay="false"
        teleport="body"
      >
        <div class="modal-close">
          <div class="icon" @click="popShow = false"></div>
        </div>
        <div class="modal-container">
          <div class="header">
            <div class="icon"></div>
          </div>
          <div class="main">
            <div class="text">{{ list[tapIndex].content }}</div>
          </div>
          <div class="footer">
            <div class="button" @click="popShow = false">
              {{ $t("I Know") }}
            </div>
          </div>
        </div>
      </van-popup>
    </div>
  </transition>
</template>

<script>
import { BalanceApi } from "@@/api/api.js";
import { ref, reactive, toRefs } from "vue";
export default {
  props: {
    hasBotGap: {
      type: Boolean,
      default: false,
    },
  },
  setup(ctx, { emit }) {
    const data = reactive({
      showNoticeScroll: false,
      list: [],
      activeIndex: 0,
      tapIndex: 1,
      popShow: false,
    });
    const getNoticeList = () => {
      BalanceApi.noticeList().then((res) => {
        data.list = res;
        // console.log("通知滚动列表为", res);
        if (res.length >= 1) {
          data.showNoticeScroll = true;
          emit("show");
        } else {
          data.showNoticeScroll = false;
        }
      });
    };

    const onChange = (i) => {
      // console.log("轮播索引为", i);
      data.activeIndex = i;
    };
    const noticeTap = (e) => {
      // console.log("点击通知栏", e, e.target.innerText, e.target);
      const el = e.target;
      console.log(
        "dom 索引为",
        el.getAttribute("data-index"),
        data.activeIndex
      );
      data.tapIndex = data.activeIndex;
      data.popShow = true;
    };

    getNoticeList();

    return {
      ...toRefs(data),
      noticeTap,
      onChange,
    };
  },
};
</script>

<style>
.van-swipe-item {
  height: 0.34rem;
  line-height: 0.34rem;
  width: 100vw;
  /*  background: #ffede4;
  color: FF793C; */
}
</style>
<style lang="less" scoped>
/* fade-transform */
.fadeDown-leave-active,
.fadeDown-enter-active {
  transition: all linear 0.2s;
}

.fadeDown-enter {
  opacity: 0;
  transform: translateY(-0.34rem);
}

.fadeDown-leave-to {
  opacity: 0;
  transform: translateY(0.34rem);
}

.cus-bar {
  margin-bottom: 0.12rem;
}

.notice-swipe {
  height: 0.34rem !important;
  line-height: 0.34rem !important;
}

.modal {
  &-container {
    background-color: white;
    border-radius: 0.16rem;
    padding: 0.28rem 0.16rem 0.28rem;
    font-size: 0.16rem;
    width: 3.11rem;
    & > .header {
      .fc();
      .icon {
        height: 0.64rem;
        width: 0.64rem;
        background: url()
          no-repeat center;
        background-size: contain;
      }
    }
    & > .main {
      margin-top: 0.08rem;
      .text {
        color: #242433;
        line-height: 0.24rem;
        text-align: center;
        font-weight: bold;
        font-size: 0.16rem;
      }
    }
    & > .footer {
      margin-top: 0.24rem;
      .fc();
      .button {
        .yowin-button();
        width: 2.64rem;
        height: 0.48rem;
        color: white;
        box-shadow: none;
      }
    }
  }
  &-close {
    display: flex;
    justify-content: flex-end;
    .icon {
      margin-bottom: 0.16rem;
      height: 0.36rem;
      width: 0.36rem;
      background: url(../../../assets/images/Modal/modal_close_white.svg)
        no-repeat center;
      background-size: contain;
    }
  }
}
</style>
